package com.fs.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.pojo.*;

import com.fs.service.*;
import com.fs.vo.Condition;
import com.fs.vo.Meta;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/reserve")
public class ReserveController {
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private InventoryService inventoryService;

    @Resource
    private ReserveService reserveService;
    @Resource
    private WareHouseService wareHouseService;
    @Resource
    private UserService userService;

    /**
     * 分页查询所有库存的方法
     *
     * @param condition
     * @return
     */
    @PostMapping("/list")
    public ResponseData<Page<Wh_inventory>> getReserve(@RequestBody Condition<Wh_inventory> condition) {
        ResponseData<Page<Wh_inventory>> responseData = new ResponseData<>();
        Page<Wh_inventory> reserve = reserveService.getReserve(condition);
        System.out.println(reserve);
        if (reserve != null) {
            responseData.setData(reserve);
            Meta meta = new Meta(200, "操作成功");
            responseData.setMeta(meta);
        } else {
            Meta meta = new Meta(404, "未找到数据");
            responseData.setMeta(meta);
        }
        return responseData;
    }

    /**
     * 添加商品库存信息的方法
     *
     * @param whInventory
     * @return
     */
    @PostMapping
    public ResponseData<Integer> AddReserve(@RequestBody Wh_inventory whInventory) {
        ResponseData<Integer> responseData = new ResponseData<>();
        Integer i = reserveService.addProduct(whInventory);
        responseData.setData(i);
        responseData.setMeta(new Meta(200, "添加成功"));
        return responseData;
    }

    /**
     * 根据商品id修改商品数量的方法
     * @return
     */
    @PutMapping
    public ResponseData<Integer> changeQuantityByProductId(@RequestBody Wh_inventory whInventory) {
        ResponseData<Integer> responseData = new ResponseData<>();
        Integer i = reserveService.changeProductQuantity(whInventory);
        responseData.setData(i);
        responseData.setMeta(new Meta(200, "修改成功"));
        return responseData;
    }

    /**
     * 根据商品id删除库存信息的方法
     *
     * @param productId
     * @return
     */
    @DeleteMapping("/{productId}")
    public ResponseData<Integer> delByProductId(@PathVariable Integer productId) {
        ResponseData<Integer> responseData = new ResponseData<>();
        Integer i = reserveService.delProductById(productId);
        responseData.setData(i);
        responseData.setMeta(new Meta(200, "删除成功"));
        return responseData;
    }

    @GetMapping("/{productId}")
    public ResponseData<Wh_inventory> getReserveById(@PathVariable Integer productId){
        return new ResponseData<>(ResultEnums.OK,reserveService.gerReserveById(productId));
    }

    /**
     * 盘点管理的列表获取
     *
     * @param condition
     * @return
     */
    @PostMapping("/check/list")
    public ResponseData<PageInfo<WhInvInventory>> checkList(@RequestBody Condition<WhInvInventory> condition) {
        return new ResponseData<PageInfo<WhInvInventory>>(ResultEnums.OK, inventoryService.selectAll(condition));
    }

    /**
     * 根据盘点单号查询盘点明细表
     *
     * @param condition
     * @return
     */
    @PostMapping("/check/queryDetail")
    public ResponseData<Page> query(@RequestBody Condition<WhInvInventoryDetail> condition) {

        return new ResponseData<>(ResultEnums.OK, inventoryDetailService.selectDetailById(condition));
    }

    /**
     * 删除盘点单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/check/delCheck/{id}")
    public ResponseData deleteCheck(@PathVariable Integer id) {
        if (inventoryService.deleteInventory(id)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    /**
     * 删除盘点明细单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/check/delDetail/{id}")
    public ResponseData deleteDetail(@PathVariable Integer id) {
        if (inventoryDetailService.deleteDetail(id)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }
    /**
     * 根据盘点单编号批量删除盘点明细单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/check/delDetailByInventory/{id}")
    public ResponseData deleteDetailByInventory(@PathVariable Integer id) {
        if (inventoryDetailService.deleteDetailByInventory(id)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    /**
     * 仓库列表
     *
     * @return
     */
    @GetMapping("/WareHouseAll")
    public ResponseData<List<WhWarehouse>> findAllWareHouse() {
        return new ResponseData<>(ResultEnums.OK, wareHouseService.queryAll());
    }

    /**
     * 审批人列表
     *
     * @return
     */
    @GetMapping("/approverAll")
    public ResponseData<List<User>> findAllApprover() {
        return new ResponseData<>(ResultEnums.OK, userService.findApprover());
    }

    /**
     * 获取最大id
     *
     * @return
     */
    @GetMapping("/maxID")
    public ResponseData<Integer> maxID() {
        return new ResponseData<>(ResultEnums.OK, inventoryService.maxID());
    }

    /**
     * 新增盘点明细
     *
     * @param condition
     * @return
     */
    @PostMapping("/addDetail")
    public ResponseData insertDetail(@RequestBody Condition<WhInvInventoryDetail> condition) {
        if (inventoryDetailService.addDetail(condition)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    /**
     * 新增盘点明细
     *
     * @param condition
     * @return
     */
    @PostMapping("/addCheck")
    public ResponseData insertCheck(@RequestBody Condition<WhInvInventory> condition) {
        if (inventoryService.addInventory(condition)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    /**
     * 根据ID获取对象
     *
     * @param id
     * @return
     */
    @GetMapping("/check/ById/{id}")
    public ResponseData<WhInvInventory> selectCheckById(@PathVariable Integer id) {
        return new ResponseData<>(ResultEnums.OK, inventoryService.selectById(id));
    }
    /**
     * 根据ID获取对象
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/ById/{id}")
    public ResponseData<WhInvInventoryDetail> selectDetailById(@PathVariable Integer id) {
        return new ResponseData<>(ResultEnums.OK, inventoryDetailService.selectById(id));
    }
    /**
     * 修改盘点明细
     *
     * @param condition
     * @return
     */
    @PutMapping("/updateDetail")
    public ResponseData updateDetail(@RequestBody Condition<WhInvInventoryDetail> condition) {
        if (inventoryDetailService.updateDetail(condition)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

    /**
     * 修改盘点明细
     *
     * @param condition
     * @return
     */
    @PutMapping("/updateCheck")
    public ResponseData updateCheck(@RequestBody Condition<WhInvInventory> condition) {
        if (inventoryService.updateInventory(condition)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }

}
